package com.carqueue.controller;/*
 * @author 86136
 * @create 2023/8/17 15:30
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.carqueue.common.Result;
import com.carqueue.pojo.User;
import com.carqueue.service.UserService;
import com.carqueue.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
@Slf4j
public class LoginController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result Login(@RequestBody User user, HttpServletRequest request){
        if(StringUtils.isNotBlank(user.getUsername()) && StringUtils.isNotBlank(user.getPassword())){
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getUsername,user.getUsername());
            User userInfo = userService.getOne(queryWrapper);

            if(userInfo == null){
                return Result.error("未找到该用户");
            }

            if(!userInfo.getPassword().equals(user.getPassword())){
                return Result.error(("密码错误"));
            }

            List<String> info = new LinkedList<>();
            info.add(userInfo.getUsername());
            info.add(Integer.toString(userInfo.getAuthority()));
            String jwt = JWTUtils.createToken(info);
            request.getSession().setAttribute("jwt", jwt);
            // 不透露密码
            userInfo.setPassword("");
            userInfo.setCarspeed(0);
            Map<String,Object> map = new HashMap<>();
            map.put("jwt",jwt);
            map.put("user",userInfo);
            return Result.success(map);
        } else {
            return Result.error("NOT_LOGIN");
        }
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    @Transactional
    public Result register(@RequestBody User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User userInfo = userService.getOne(queryWrapper);
        if(userInfo != null){
            return Result.error("REPEAT");
        } else {
            user.setAuthority(0);
            userService.save(user);
            return Result.success(null);
        }
    }

    /**
     * 修改密码--校验用户信息，成功则将用户信息保存至session
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/findpassword")
    public Result findPassword(@RequestBody User user, HttpServletRequest request){
        if(StringUtils.isNotBlank(user.getUsername()) && StringUtils.isNotBlank(user.getName()) && StringUtils.isNotBlank(user.getPhonenum()) && StringUtils.isNotBlank(user.getBirthdate())){
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getUsername,user.getUsername());
            queryWrapper.eq(User::getName,user.getName());
            queryWrapper.eq(User::getPhonenum,user.getPhonenum());
            queryWrapper.eq(User::getBirthdate,user.getBirthdate());

            User userInfo = userService.getOne(queryWrapper);
            if(userInfo == null){
                return Result.error("信息输入错误");
            } else {
                request.getSession().setAttribute("user",user);
                return Result.success(null);
            }
        } else {
            return Result.error("信息输入错误");
        }
    }

    /**
     * 修改密码
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/changepassword")
    @Transactional
    public Result changePassword(@RequestBody User user, HttpServletRequest request){
        User userInfo = (User) request.getSession().getAttribute("user");
        if(userInfo != null){
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getUsername,userInfo.getUsername());
            queryWrapper.eq(User::getName,userInfo.getName());
            queryWrapper.eq(User::getPhonenum,userInfo.getPhonenum());
            queryWrapper.eq(User::getBirthdate,userInfo.getBirthdate());
            User user1 = userService.getOne(queryWrapper);
            user1.setPassword(user.getPassword());
            userService.updateById(user1);
            request.getSession().removeAttribute("user");
            return Result.success(null);
        } else {
            return Result.error("ERROR！");
        }
    }
}
